import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:nyh_motorcade_app/widgets/text_button_with_icon.dart';
import 'package:nyh_motorcade_app/widgets/text_with_icon.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'driver_code_controller.dart';

class DriverCodePage extends StatelessWidget {
  final controller = Get.put(DriverCodeController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(context: context, title: "司机开户码"),
      body: GetBuilder<DriverCodeController>(
        builder: (controller) {
          return Column(
            children: [
              Container(
                width: double.infinity,
                margin: EdgeInsets.only(left: 16.w, top: 65.w, right: 16.w),
                padding: EdgeInsets.symmetric(vertical: 18.w, horizontal: 16.w),
                decoration: BoxDecoration(
                  color: Colours.color_white,
                  borderRadius: BorderRadius.circular(10.w),
                  boxShadow: [
                    BoxShadow(
                      color: Colours.color_0f112e_95,
                      offset: Offset(0.0, 2.w),
                      blurRadius: 5.w,
                    ),
                  ],
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    TextWithIcon(
                      text: controller.companyName,
                      textStyle: TextStyle(
                        color: Colours.color_text_theme,
                        fontSize: 14.sp,
                      ),
                      iconName: "images/ic_company.png",
                      iconColor: Colours.color_text_theme,
                      textIconPadding: 5.w,
                    ),
                    SizedBox(
                      height: 10.w,
                    ),
                    TextButtonWithIcon(
                      onPressed: () {},
                      icon: Image.asset(
                        "images/ic_arrow_right_gray.png",
                        color: Colours.color_theme,
                      ),
                      label: Text(
                        controller.motorcadeName.isNotEmpty ? controller.motorcadeName : "请选择所属车队",
                        style: TextStyle(
                          color: Colours.color_theme,
                          fontSize: 14.sp,
                        ),
                      ),
                      textIconPadding: 5.w,
                      iconTextAlignment: IconTextAlignment.iconRightTextLeft,
                      style: ButtonStyle(
                        tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                        minimumSize: MaterialStateProperty.all(Size(0, 0)),
                        padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                      ),
                    ),
                    SizedBox(
                      height: 15.w,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        SizedBox(
                          width: 10.w,
                        ),
                        TextButton(
                          onPressed: () {
                            controller.changeDriverType(true);
                          },
                          child: Text(
                            "自有司机",
                            style: TextStyle(
                              color: controller.isOwnDriver ? Colours.color_text_theme : Colours.color_9fa0ab,
                              fontSize: controller.isOwnDriver ? 16.sp : 14.sp,
                              fontWeight: controller.isOwnDriver ? FontWeight.bold : FontWeight.normal,
                            ),
                          ),
                          style: ButtonStyle(
                            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                            minimumSize: MaterialStateProperty.all(Size(0, 0)),
                            padding: MaterialStateProperty.all(EdgeInsets.all(5)),
                          ),
                        ),
                        TextButton(
                          onPressed: () {
                            controller.changeDriverType(false);
                          },
                          child: Text(
                            "外协司机",
                            style: TextStyle(
                              color: !controller.isOwnDriver ? Colours.color_text_theme : Colours.color_9fa0ab,
                              fontSize: !controller.isOwnDriver ? 16.sp : 14.sp,
                              fontWeight: !controller.isOwnDriver ? FontWeight.bold : FontWeight.normal,
                            ),
                          ),
                          style: ButtonStyle(
                            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                            minimumSize: MaterialStateProperty.all(Size(0, 0)),
                            padding: MaterialStateProperty.all(EdgeInsets.all(5)),
                          ),
                        ),
                        SizedBox(
                          width: 10.w,
                        ),
                      ],
                    ),
                    SizedBox(
                      height: 16.w,
                    ),
                    Container(
                      child: Center(
                        child: Stack(
                          alignment: AlignmentDirectional.center,
                          children: [
                            Visibility(
                              visible: !controller.isCreatedCode,
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Colours.color_white,
                                  border: Border.all(width: 1.w, color: Colours.color_white),
                                ),
                                padding: EdgeInsets.all(10.w),
                                child: Image.asset(
                                  "images/ic_qr_code.png",
                                  width: 212.w,
                                  height: 212.w,
                                  fit: BoxFit.cover,
                                ),
                              ),
                            ),
                            Visibility(
                              visible: controller.isCreatedCode,
                              child: RepaintBoundary(
                                key: controller.repaintGlobalKey,
                                child: Container(
                                  decoration: BoxDecoration(
                                    color: Colours.color_white,
                                    border: Border.all(width: 1.w, color: Colours.color_white),
                                  ),
                                  padding: EdgeInsets.all(10.w),
                                  child: QrImage(
                                    data: controller.qrCodeData,
                                    version: QrVersions.auto,
                                    size: 212.w,
                                    padding: EdgeInsets.all(0),
                                  ),
                                ),
                              ),
                            ),
                            Visibility(
                              visible: !controller.isCreatedCode,
                              child: TextButton(
                                onPressed: () {
                                  controller.createQrCode("123456789");
                                },
                                child: Text(
                                  "生成二维码",
                                  style: TextStyle(
                                    color: Colours.color_white,
                                    fontSize: 14.sp,
                                  ),
                                ),
                                style: ButtonStyle(
                                  backgroundColor: MaterialStateProperty.resolveWith((states) {
                                    return controller.motorcadeId.isNotEmpty ? Colours.color_theme : Colours.color_ffd599;
                                  }),
                                  overlayColor: MaterialStateProperty.resolveWith((states) {
                                    return controller.motorcadeId.isNotEmpty ? Colours.color_theme_deep : Colours.color_ffd599;
                                  }),
                                  shape: MaterialStateProperty.all(
                                    RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(5.w),
                                    ),
                                  ),
                                  minimumSize: MaterialStateProperty.all(Size(118.w, 40.w)),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    SizedBox(
                      height: 16.w,
                    ),
                    Visibility(
                      visible: controller.isCreatedCode,
                      child: Center(
                        child: Text(
                          "请司机扫描上方二维码完成开户",
                          style: TextStyle(
                            color: Colours.color_9fa0ab,
                            fontSize: 12.sp,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Visibility(
                visible: controller.isCreatedCode,
                child: Container(
                  margin: EdgeInsets.only(top: 5.w, right: 11.w),
                  alignment: Alignment.centerRight,
                  child: TextButtonWithIcon(
                    onPressed: () {
                      controller.saveQrCode();
                    },
                    icon: Image.asset("images/ic_download.png"),
                    label: Text(
                      "保存二维码到相册",
                      style: TextStyle(
                        color: Colours.color_text_theme,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    textIconPadding: 5.w,
                    style: ButtonStyle(
                      tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                      minimumSize: MaterialStateProperty.all(Size(0, 0)),
                      padding: MaterialStateProperty.all(EdgeInsets.all(5.w)),
                    ),
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
